<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ $t('user.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="el-icon-back" @click="$router.go(-1)">
              {{ $t('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="100px" ref="dataForm" :model="dataForm" :rules="dataRule">

          <el-form-item :label="$t('user.username')" prop="username">
            <el-input v-model="dataForm.username" :placeholder="$t('user.please_mobile')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('common.default_password')">
            <el-tag type="danger">123456</el-tag>
          </el-form-item>

          <el-form-item prop="role_id" :label="$t('user.role.title')" class="width_auto">
            <el-radio-group v-model="dataForm.role_id">
              <el-radio v-for="(v,k) in rolelist" :key="k" :label="v.id">
                {{v.title}}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item :label="$t('user.nickname')" prop="nickname">
            <el-input v-model="dataForm.nickname" :placeholder="$t('common.please_input')+$t('user.nickname')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('user.avatar')" prop="avatar">
            <el-upload class="avatar-uploader" :action="this.$http.adornUrl('/file/picture')" :show-file-list="false" :headers="upload_headers" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar-upload">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item :label="$t('user.mobile')" prop="mobile">
            <el-input v-model="dataForm.mobile" :placeholder="$t('common.please_input')+$t('user.mobile')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('user.email')" prop="email">
            <el-input v-model="dataForm.email" :placeholder="$t('common.please_input')+$t('user.email')"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('user:handle')" type="primary" @click="dataFormSubmit()">
              {{ $t('common.confirm') }}
            </el-button>
            <el-button @click="resetForm()">
              {{ $t('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import common from '@/views/common/base'
  export default {
    extends: common,
    data() {
      return {
        model: 'user',
        rolelist: [],
        upload_headers:{},
        dataForm:
        {
          id: 0,
          role_id : '',
          username: '',
          nickname: '',
          avatar: '',
          mobile: '',
          email: '',
          sms_notification: true,
        },
        dataRule:
        {
          username: [
            { required: true, message: this.$t('user.rules.username.require'), trigger: 'blur' },
          ],
          nickname: [
            { required: true, message: this.$t('user.rules.nickname.require'), trigger: 'blur' },
          ],
          role_id: [
            { required: true, message: this.$t('user.rules.role.require'), trigger: 'blur' },
          ],
        }
      };
    },
    methods: {
      init ()
      {
        let query = this.$route.query
        let id = query.id

        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/view/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.status === 200) {
                this.dataForm.role_id  = data.data.role_id
                this.dataForm.username = data.data.username
                this.dataForm.nickname = data.data.nickname
                this.dataForm.avatar   = data.data.avatar
                this.dataForm.mobile   = data.data.mobile
                this.dataForm.email    = data.data.email
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/handle`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'role_id': this.dataForm.role_id,
                'username': this.dataForm.username,
                'nickname': this.dataForm.nickname,
                'avatar': this.dataForm.avatar,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
              })
            }).then(({data}) => {
              if (data && data.status === 200) {
                this.$message.success(this.$t('common.handle_success'));
                this.$router.go(-1);
              } else {
                this.$message.error(this.$t(data.message))
              }
            })
          }
        })
      },
      loadRoleList () {
        this.$http({
          url: this.$http.adornUrl('/role/select'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.status === 200) {
            this.rolelist = data.data
          } else {
            this.$message.error(this.$t(data.message))
          }
        })
      },
      resetForm:function()
      {
        this.$refs['dataForm'].resetFields();
      },
    },
    created() {
      this.init();

      // 要保证取到
      this.upload_headers.Authorization = 'Bearer ' + localStorage.getItem('token');
    },
    mounted () {
      this.loadRoleList();
    },
  };
</script>
